//
//  TengineModel.m
//  TengineTwo
//
//  Created by StandardUser on 10/02/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
//
//

#import "TengineModel.h"
#import "ClockModel.h"

@interface TengineModel()

@property(readwrite, nonatomic, retain) ClockModel* clock;
@property(readonly, nonatomic) NSMutableDictionary *delegates; //'constant'
-(void)updateT;
-(void)moveDelegates;

@end

@implementation TengineModel

@synthesize clock;
@synthesize delegates = _delegates;
@synthesize t = _t;

- (id)init {
    self = [super init];
    if (self) {
        clock = [[ClockModel alloc] initWithFPS:60];
        _t = 0;
        _delegates = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)dealloc {
    [clock release];
    [_delegates release];
    [super dealloc];
}

#pragma mark accessors

-(void)setClockMultiplier:(float)value
{
    clock.clockMultiplier = value;
}

-(float)clockMultiplier
{
    return clock.clockMultiplier;
}

#pragma mark -

-(void)updateT
{
    _t = [clock update];
}

-(void)update
{
    [self updateT];
    [self moveDelegates];
}

-(void)addEntity:(id<TengineDelegate>)entity
{
    entity.cachedTag = [NSString stringWithFormat:@"tag%i", _delegates.count];
    [_delegates setObject:entity forKey:entity.cachedTag]; //adds retain
}

-(void)removeEntityByTag:(NSString*)tag
{
    [_delegates removeObjectForKey:tag];
}

-(void)moveDelegates
{
    [_delegates enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
        id<TengineDelegate> d = (id<TengineDelegate>)obj;
        [d updateFromT:_t];
    }];
}

@end
